﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Get;
using Traceability.App.Api.DTO;
using Traceability.App.Domain;
using Traceability.App.ErrorCode;
using Traceability.App.Instructure;

namespace Traceability.App.Api.Application.Handler.Get
{
    public class GetProductHandler : IRequestHandler<GetProductCommand, ApiResult<List<ProductDTO>>>
    {
        private readonly IBaseRepository<ProductModel> productRepo;
        private readonly IMapper mapper;

        public GetProductHandler(IBaseRepository<ProductModel> productRepo,IMapper mapper) 
        {
            this.productRepo = productRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<ProductDTO>>> Handle(GetProductCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ProductDTO>> res = new ApiResult<List<ProductDTO>>();
            var product = productRepo.GetAll().Where(x => x.IsDelete == false).ToList();
            res.Data=mapper.Map<List<ProductDTO>>(product);
            res.Code = ApiEnum.Success;
            res.Msg = "查询成功";
            return Task.FromResult(res);
        }
    }
}
